package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.common.aliyun.GreenTextScan;
import com.heima.common.exception.CustomException;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmSensitive;
import com.heima.utils.common.SensitiveWordUtil;
import com.heima.wemedia.mapper.WmSensitiveMapper;
import com.heima.wemedia.service.ICommentInspectService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@Service
public class CommentInspectServiceImpl implements ICommentInspectService {

    @Autowired
    private GreenTextScan greenTextScan;

    @Autowired
    private WmSensitiveMapper sensitiveMapper;

    @Override
    public ResponseResult commentContentInspect(String content) {

        System.out.println(content);
        Map<String, Integer> sensitivesMap = handleCommentSensitives(content);
        log.info("文本敏感词审核：{}", sensitivesMap);
        log.info(String.valueOf(sensitivesMap.size()));

        if (sensitivesMap.size() > 0) {
            log.info("有敏感词");
            throw new CustomException(AppHttpCodeEnum.RUBBISH_COMMENT);
        }

        Map map = handleCommentAli(content);
        log.info("文本审核结果：{}", map);
        String suggestion = (String) map.get("suggestion");
        String lable = (String) map.get("label");
        if ("block".equals(suggestion)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.RUBBISH_COMMENT, lable);
        }
        return ResponseResult.okResult(null);
    }


    /**
     * 评论内容敏感词审核
     *
     * @return
     */
    private Map<String, Integer> handleCommentSensitives(String commentContent) {
        log.info("评论内容{}", commentContent);
        /**
         * 查询敏感词
         */
        List<WmSensitive> wmSensitives =
                sensitiveMapper.selectList(Wrappers.<WmSensitive>lambdaQuery().select(WmSensitive::getSensitives));
        if (CollectionUtils.isEmpty(wmSensitives)) {
            return null;
        }
        List<String> sensitiveList =
                wmSensitives.stream().map(WmSensitive::getSensitives).collect(Collectors.toList());
        /**
         * 初始化敏感词
         */
        SensitiveWordUtil.initMap(sensitiveList);
        /**
         * 审核敏感词
         */
        Map<String, Integer> reslutMap = SensitiveWordUtil.matchWords(commentContent);
        return reslutMap;
    }

    /**
     * 评论内容阿里云检测
     *
     * @param commentContent
     */
    private Map handleCommentAli(String commentContent) {
        Map map = null;
        try {
            map = greenTextScan.greeTextScan(commentContent);
        } catch (Exception e) {
            log.error("阿里云文本扫描异常", e);
        }
        return map;
    }
}
